﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OpenQuarters.WebQuarters.Utils;
using System.Collections;
using System.Reflection;

namespace OpenQuarters.WebQuarters.Core.Web.Areas.CMS.Controllers
{
    public class InfoController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetOptionList()
        {
            ViewData.Model = "[]";
            List<object> options = Resources.GetOptionList(Server.UrlDecode(Request.Form["source"]));
            
            ViewData.Model = options.ToJSON();
            return View(Site.Helpers.SharedView.Service);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MethodHandler(string id)
        {
            ViewData.Model = "{}";

            string[] args = id.Split(',');

            if (args.Length < 3)
                throw new ArgumentException("The assembly name, module name and method must be passed to the handler seperated by a comma");

            string assemblyName = args[0];
            string moduleName = args[1];
            string method = args[2];

            Assembly assembly = Assembly.Load(assemblyName);
            Type moduleType = GeneralUtils.GetTypeByName(assemblyName + "." + moduleName);

            MethodInfo methodInfo = moduleType.GetMethod(method, new Type[] { });

            if (methodInfo == null)
            {
                throw new ArgumentException("Specified method not found");
            }

            // Check that method implements InvokableAttribute
            if (methodInfo.GetCustomAttributes(typeof(InvokableAttribute), true).Count() == 0)
            {
                throw new ArgumentException("The method being called must have an InvokableAttribute");
            }

            var returnObj = methodInfo.Invoke(assembly, new object[] { });
            if (returnObj != null)
            {
                ViewData.Model = returnObj.ToJSON();
            }
            else
            {
                ViewData.Model = "{}";
            }
            return View(Site.Helpers.SharedView.Service);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult MethodHandlerView(string id)
        {
            string[] parameters = id.Split('/');

            if (parameters.Length < 2)
                throw new ArgumentException("The method name and view must be seperated by a forward slash");

            string view = id.Substring(parameters[0].Length);

            string[] args = parameters[0].Split(',');

            if (args.Length < 3)
                throw new ArgumentException("The assembly name, module name and method must be passed to the handler seperated by a comma");

            string assemblyName = args[0];
            string moduleName = args[1];
            string method = args[2];
            //string view = parameters[1];

            Assembly assembly = Assembly.Load(assemblyName);
            Type moduleType = GeneralUtils.GetTypeByName(assemblyName + "." + moduleName);

            MethodInfo methodInfo = moduleType.GetMethod(method);

            if (methodInfo == null)
            {
                throw new ArgumentException("Specified method not found");
            }

            // Check that method implements InvokableAttribute
            if (methodInfo.GetCustomAttributes(typeof(InvokableAttribute), true).Count() == 0)
            {
                throw new ArgumentException("The method being called must have an InvokableAttribute");
            }   

            var returnObj = methodInfo.Invoke(assembly, new object[] { Request });
            if (returnObj != null)
            {
                ViewData.Model = returnObj;
            }

            var viewPath = view;
            if (!string.IsNullOrEmpty(Request["itemType"]))
                viewPath = "~/Templates/" + GeneralUtils.GetTypeByName(Request["itemType"]).FullName + view + ".ascx";

            return View(viewPath);
        }

    }
}
